package com.butterfly.vv.drag;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import com.beem.project.beem.R;
import com.butterfly.vv.EditorActivity;
import com.butterfly.vv.choose.CustomRelativeLayout;
import com.pixem.core.activity.Filter;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

/*
Demonstration of one way to put a set of draggable symbols on screen. Define symbols as image 
files in res/drawable-hdpi/filename.png (or .jpg or .gif files). Refer to them then by 
R.drawable.filename in the array symbolIndex, and give the initial x and y coordinates for 
each symbol in the arrays X and Y.
*/

public class DraggableSymbols extends Activity {
	
	private LinearLayout root_layout;
	RelativeLayout mRelative;
	DragginSlayer view;
	
	 String inamgeuri = "content://media/external/images/media/";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        //20121225
        setContentView(R.layout.test_nothing);
        //20121225
        
        //20121219
        ArrayList<String> imageids = (ArrayList<String>) getIntent().getSerializableExtra("imageids");
       Log.i("VV", "tt "+imageids);
       ArrayList<Drawable> drawables = new ArrayList<Drawable>();
       for (Iterator iterator = imageids.iterator(); iterator.hasNext();) {
		String string = (String) iterator.next();
		Bitmap b;
		try {
			//b = android.provider.MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(string));
            b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong(string.replace(inamgeuri, "")), Images.Thumbnails.MICRO_KIND, null);

			Drawable drawable= new BitmapDrawable(b);
			drawables.add(drawable);
			//Resources resources = new Resources();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
	}
        //20121229
        
        // Define the symbols and their initial coordinates in arrays. No limit in principle 
        // to how many. Coordinates are measured from the upper left corner of the screen, 
        // with x increasing to the right and y increasing downward
        
        float [] X = {2, 148};   // Initial x coord in pixels of upper left corner of symbol
        float [] Y = {2, 2};     // Initial y coord in pixels of upper left corner of symbol
        
        // The Drawable corresponding to the symbol. R.drawable.file refers to file.png, .jpg, 
        // or .gif stored in res/drawable-hdpi (referenced from code without the extension).
        
        int[]symbolIndex = {R.drawable.sample_00, R.drawable.sample_02, R.drawable.ic_launcher};
        
        // Instantiate a SymbolDragger instance (which subclasses View), passing to it in the
        // constructor the context (this) and the above arrays.  Then set the content view to 
        // this instance of SymbolDragger (so the layout is being specified entirely by SymbolDragger, 
        // with no XML layout file).  The resulting view should then place draggable symbols with 
        // content and initial position defined by the above arrays on the screen.
        
        //SymbolDragger view = new SymbolDragger(this, X, Y, symbolIndex);//
        //DragginSlayer view = new DragginSlayer(this, X, Y, symbolIndex);
         //view = new DragginSlayer(this, X, Y, symbolIndex);
        view = new DragginSlayer(this, X, Y, drawables);
         
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
             ViewGroup.LayoutParams.FILL_PARENT));
       
        //setContentView(R.layout.automatic_layout_activity);
        
        root_layout = (LinearLayout) findViewById(R.id.root_test);
        root_layout.setBackgroundColor(15921906);
        mRelative = new CustomRelativeLayout(this);
        mRelative.setBackgroundColor(Color.rgb(242, 242, 242));
        LayoutParams fill = new LayoutParams(LayoutParams.FILL_PARENT, 650);
        mRelative.setLayoutParams(fill);
        root_layout.addView(mRelative);
        
//        RelativeLayout mControls = new RelativeLayout(this);
//        mControls.setMinimumHeight(55);
//        LayoutParams controlParams = null;
//        controlParams = new LayoutParams(LayoutParams.FILL_PARENT, 50);
//        controlParams.bottomMargin = 10;
//        controlParams.addRule(RelativeLayout.ABOVE, 1);
//        mControls.setLayoutParams(controlParams);
//        mControls.addView(new Button(this));
        LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT, android.widget.LinearLayout.LayoutParams.FILL_PARENT, 1);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.addView(view);
        
        LinearLayout.LayoutParams layoutParams2 =new LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT, android.widget.LinearLayout.LayoutParams.FILL_PARENT);
        LinearLayout linearLayout2 = new LinearLayout(this);
       // layoutParams2.
        linearLayout2.setLayoutParams(layoutParams2);
        linearLayout2.addView(new Button(this));
        
        LayoutInflater inflater = LayoutInflater.from(this);
        View mpView = inflater.inflate(R.layout.bottom_bar, root_layout);
        ImageButton cancel_button = (ImageButton) mpView.findViewById(R.id.cancel_button);
        ImageButton ok_button = (ImageButton) mpView.findViewById(R.id.ok_button);
        cancel_button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent kk = new Intent(getApplicationContext(),Filter.class);
				//kk.putExtra("uri", inamgeuri+838);
				String uri = "file://"+view.getForuri();
				kk.putExtra("uri", uri);
				startActivity(kk);
				//finish();
			}
		});
        ok_button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//Log.i("VV", );
				String uri = "file://"+view.getForuri();
				Intent intent = new Intent(getApplicationContext(),EditorActivity.class);
				intent.putExtra(getString(R.string.image_uri_flag), (uri));
				startActivity(intent);
				//finish();
			}
		});
        
        mRelative.addView(linearLayout);
        //mRelative.addView(linearLayout2);
        //root_layout.addView(linearLayout);
        //root_layout.addView(linearLayout2);
        //mRelative.addView(mControls);
        //mRelative.addView(view);
        //setContentView(view);
    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	
    }
}